<?php
include( "agent/onlinebet_header5.php" );
$bettype = 7;
$userid = $curruser['id'];
$theacl = $curruser['acl'];
$bet_usersql = "";
$bet2_usersql = "";
$bet8_usersql = "";
if ($curruser['acl'] != 255)
{
    $userlist = GetUserList($curruser['id']);
    if ($userlist == "")
    {
        $ui->showErrorPage("<p>还没有建立会员！</p>");
        exit();
    }
    else
    {
        $bet_usersql = " and user_id in ({$userlist})";
        $bet2_usersql = " and uperid{$theacl}={$userid}";
        $bet8_usersql = " and user_id={$userid}";
    }
    if ($curruser['acl'] == 2)
    {
        $bet2_usersql = " and user_id=0 ";
    }
}
$g_disk_code = array(1 => "a", 2 => "b", 3 => "c", 4 => "d");
$disk_code = "a";
$disksql = "";
if (0 < $disk)
{
    $disksql = " and disk=" . $disk;
    $disk_code = $g_disk_code[$disk];
}
$bet_percent = "";
$bet2_percent = "";
$bet28 = "bet2";
if ($split == 1)
{
    switch ($theacl)
    {
        case 2 :
            $bet_percent = "*mpercent/100";
            $bet2_percent = "";
            break;
        case 3 :
            $bet_percent = "*percent2/100";
            $bet2_percent = "*percent3/100";
            break;
        case 4 :
            $bet_percent = "*percent3/100";
            $bet2_percent = "*percent4/100";
            break;
        case 5 :
            $bet_percent = "*percent4/100";
            $bet2_percent = "*percent5/100";
            break;
        default :
            $bet_percent = "*upercent/100";
            $bet2_percent = "*percent255/100";
            $bet28 = "bet8";
            break;
    }
}
$rowNum = 20;
$colNum = 10;
$tot = $rowNum * $colNum;
$percent = 0;
$lostwin = array();
$betcount = array();
$totalamount = array();
$list = array();
$totalamount[$bettype] = 0;
$betcount[$bettype] = 0;
$list[$bettype] = "";
$betTypeStr = "";
switch ($bettype)
{
    case 9 :
        $betTypeStr = "bettype IN ( 9,33)";
        break;
    case 10 :
        $betTypeStr = "bettype IN (10,32)";
        break;
    default :
        $betTypeStr = "bettype=" . $bettype;
        break;
}
$sql = " sum(totalAmt) as totalAmt FROM (\r\n(Select line_nums,(unitmoney{$bet_percent}) as totalAmt\r\nfrom bet where {$betTypeStr} and lotto_id={$lotto_id} {$bet_usersql} and rame in (0,9) {$disksql})\r\nUNION ALL\r\n(Select line_nums,(unitmoney{$bet2_percent}) as totalAmt\r\nfrom bet2 where {$betTypeStr} and lotto_id={$lotto_id} {$bet2_usersql} and rame in (0,9))\r\nUNION ALL\r\n(Select line_nums,(amount*-1) as totalAmt\r\nfrom {$bet28} where {$betTypeStr} and lotto_id={$lotto_id} {$bet8_usersql} and rame in (0,9))\r\n) A";
$rs1 = getdb("SELECT " . $sql);
$rs2 = getdb("Select line_nums," . $sql . " GROUP BY line_nums Order by totalAmt desc LIMIT " . $tot);
$totalamount[$bettype] = round(intval($rs1->f[0]), 0);
$list = array();
if ($totalamount[$bettype] != 0)
{
    $col = 0;
    $recc = 0;
    while (!$rs2->eof)
    {
        $c = $rs2->f[0];
        $n = round($rs2->f[1], 0);
        if ($recc == 0)
        {
            $list[$col] = "<table border=0 width=88% cellspacing=0 cellpadding=0>";
        }
        if ($c != "" && $n != 0)
        {
            $list .= $col;
            $list .= $col;
            $list .= $col;
        }
        if ($rowNum - 1 <= $recc)
        {
            $list .= $col;
            ++$col;
            $recc = 0;
        }
        else
        {
            ++$recc;
        }
        $rs2->movenext();
    }
    if (0 < $recc && $recc < $rowNum - 1)
    {
        $list .= $col;
    }
}
$rs1->close();
$rs2->close();
$rs = getdb("select * from rate5" . $disk_code . " where lotto_id=" . $lotto_id);
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>系统目前维护中，请稍後！</p>");
    exit();
}
$tpl->set("r7_1", $rs->f['r7_1']);
$i = 0;
while ($i < count($list))
{
    $tpl->set("list" . $i, $list[$i]);
    ++$i;
}
$tpl->set("totall", "显示排行前 <b>" . $tot . "</b> 笔下注资料");
$tpl->load("main", "onlinebet5_7.htm");
$tpl->show("main");
$ui->footer();